﻿using System.Runtime.InteropServices;
using System.Text;

namespace Minecraft_Armor_Stand_Creater.Util
{
    class ConfigOperator
    {
        /// <summary>
        /// 配置文件默认路径
        /// </summary>
        const string iniPath = @".\config.ini";
        public static StringBuilder StrName = new(255);//用于存储的字符串  

        #region ini functions 
        /// <summary>
        /// 写入指定数据到配置文件
        /// </summary>
        /// <param name="section">节名</param>
        /// <param name="key">键名</param>
        /// <param name="val">键值</param>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        [DllImport("kernel32", CharSet = CharSet.Unicode)]
        static extern void WritePrivateProfileString(string section, string key, string val, string filePath);

        /// <summary>
        /// 获取小节键的值，保存到字符串
        /// </summary>
        /// <param name="section">节名</param>
        /// <param name="key">键名</param>
        /// <param name="def"></param>
        /// <param name="retVal">结果返回的字符串变量</param>
        /// <param name="size">大小</param>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        [DllImport("kernel32", CharSet = CharSet.Unicode)]
        static extern void GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        #endregion

        /// <summary>
        /// 读取配置文件
        /// </summary>
        /// <param name="StrName">字符串名</param>
        /// <param name="configureNode">节名</param>
        /// <param name="key">键名</param>
        /// <param name="path">文件路径</param>
        public static string IniRead(string configureNode, string key, string path = iniPath)
        {
            GetPrivateProfileString(configureNode, key, "", StrName, 255, path);
            return StrName.ToString();
        }

        /// <summary>
        /// 写入配置文件
        /// </summary>
        /// <param name="configureNode">节名</param>
        /// <param name="key">键名</param>
        /// <param name="keyValue">键值</param>
        /// <param name="path">文件路径</param>
        public static void IniWrite(string configureNode, string key, string keyValue, string path = iniPath)
        {
            WritePrivateProfileString(configureNode, key, keyValue, path);
        }
    }
}
